﻿#region using
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
#endregion

namespace AwesomeTanks.Sources.PlayerInputs
{
    /// <summary>
    /// GamePad controls to play to AwesomeTanks
    /// </summary>
    public class GamePadControlsState : IControlsState
    {
        #region Default Controls

        private const Buttons DEPLOY_ITEM_DEFAULT_BUTTON = Buttons.A;
        private const Buttons SHOT_DEFAULT_BUTTON = Buttons.RightTrigger;
        private const Buttons SPECIAL_SHOT_DEFAULT_BUTTON = Buttons.LeftTrigger;
        private const Buttons PAUSE_DEFAULT_BUTTON = Buttons.Start;

        public const double PLAYER_DEATH_VIBRATION_DURATION_MS = 2000;
        public const double PLAYER_DEATH_VIBRATION_INTENSITY = 1;
        public const double TANK_DESTRUCTION_VIBRATION_DURATION_MS = 1000;
        public const double TANK_DESTRUCTION_VIBRATION_INTENSITY = 0.5;

        #endregion

        #region Fields

        public PlayerIndex PlayerIndex { get; set; }
        public TankAction PreviousTankAction { get; set; }
        public double CurrentVibrationDuration { get; set; }

        #endregion

        #region Constructors

        public GamePadControlsState(PlayerIndex playerIndex)
        {
            this.PlayerIndex = playerIndex;
        }

        #endregion

        #region Methods

        public Vector2 GetTankDirection()
        {
            Vector2 direction = new Vector2(0, 0);
            GamePadState gamepad = GamePad.GetState(this.PlayerIndex);

            if (gamepad.IsConnected)
            {
                direction = gamepad.ThumbSticks.Left * new Vector2(1, -1);
            }

            return direction;
        }

        public Vector2 GetTankAiming()
        {
            Vector2 aiming = new Vector2(0, 0);
            GamePadState gamepad = GamePad.GetState(this.PlayerIndex);

            if (gamepad.IsConnected)
            {
                aiming = gamepad.ThumbSticks.Right;
            }

            return aiming;
        }

        public TankAction GetTankAction()
        {
            TankAction action = TankAction.NONE;
            GamePadState gamepad = GamePad.GetState(this.PlayerIndex);

            if (gamepad.IsConnected)
            {
                if (gamepad.IsButtonDown(PAUSE_DEFAULT_BUTTON) && PreviousTankAction != TankAction.PAUSE)
                {
                    action = TankAction.PAUSE;
                }
                else if (gamepad.IsButtonDown(SPECIAL_SHOT_DEFAULT_BUTTON) && PreviousTankAction != TankAction.SPECIAL_SHOT)
                {
                    action = TankAction.SPECIAL_SHOT;
                }
                else if (gamepad.IsButtonDown(DEPLOY_ITEM_DEFAULT_BUTTON) && PreviousTankAction != TankAction.DEPLOY_ITEM)
                {
                    action = TankAction.DEPLOY_ITEM;
                    
                }
                else if( gamepad.IsButtonDown(SHOT_DEFAULT_BUTTON) && PreviousTankAction != TankAction.SHOT) 
                {
                    action = TankAction.SHOT;
                }
            }
            
            PreviousTankAction = action;
            return action;
        }

        public Boolean IsConnected()
        {
            return GamePad.GetState(this.PlayerIndex).IsConnected;
        }

        public void StartVibration(double vibrationToMiliSeconds, double intensity)
        {
            this.CurrentVibrationDuration = vibrationToMiliSeconds;
            GamePad.SetVibration(this.PlayerIndex, (float)intensity, (float)intensity);
        }

        public void StopVibration()
        {
            this.CurrentVibrationDuration = 0;
            GamePad.SetVibration(this.PlayerIndex, 0, 0);
        }

        public void Update(GameTime gameTime)
        {
            if (CurrentVibrationDuration > 0)
            {                
                this.CurrentVibrationDuration -= gameTime.ElapsedGameTime.TotalMilliseconds;
            }
            else
            {
                GamePad.SetVibration(this.PlayerIndex, 0, 0);
            }
        }

        #endregion
    }
}
